/*
 * ImageController.cpp
 *
 *  Created on: Nov 29, 2009
 *      Author: tptfc
 */

#include "ImageController.h"
#include "../utils/PgmFileUtils.h"
#include "../utils/BmpFileUtils.h"
#include "../exception/InvalidImageExtensionException.h"

IImageController *ImageController::instance = new ImageController();

ImageController::ImageController() {
}

ImageController::~ImageController() {
}

IImageController *ImageController::getInstance() {
	return instance;
}

ImageEntity *ImageController::getImage(string imagePath) {
	if (StringUtils::endWith(imagePath, "pgm") || StringUtils::endWith(imagePath, "PGM")) {
		return PgmFileUtils::read(imagePath);
	} else if (StringUtils::endWith(imagePath, "bmp") || StringUtils::endWith(imagePath, "BMP")) {
		return BmpFileUtils::read(imagePath);
	} else {
		throw new InvalidImageExtensionException();
	}
}

void ImageController::saveImage(string originalPath, string imagePath, ImageEntity *imageEntity) {
	if (StringUtils::endWith(imagePath, "pgm") || StringUtils::endWith(imagePath, "PGM")) {
		PgmFileUtils::save(imagePath, imageEntity);
	} else if (StringUtils::endWith(imagePath, "bmp") || StringUtils::endWith(imagePath, "BMP")) {
		BmpFileUtils::write(originalPath, imagePath, imageEntity);
	} else {
		throw new InvalidImageExtensionException();
	}
}

